﻿
Partial Class pages_products_ProductAdd
    Inherits CPage

#Region "Querystring"
    Public ReadOnly Property TypeId() As Byte
        Get
            Return CWeb.RequestByte("typeId", EProductType.All)
        End Get
    End Property
    Public ReadOnly Property BelongsToId() As Integer
        Get
            Return CWeb.RequestInt("belongsToId")
        End Get
    End Property
#End Region

#Region "Members"
    Private m_product As CProduct
#End Region

#Region "Data"
    Public ReadOnly Property [Product]() As CProduct
        Get
            If IsNothing(m_product) Then
                m_product = New CProduct

                m_product.ProductTypeId = TypeId
                m_product.ProductBelongsToProductId = BelongsToId

                m_product.ProductCreated = DateTime.Now.Date
                If Not IsNothing(m_product.BelongsTo) Then
                    m_product.ProductCreated = m_product.BelongsTo.ProductCreated
                    m_product.ProductBelongsToSortOrder = m_product.BelongsTo.Children.Count
                    m_product.ProductSpeakerId = m_product.BelongsTo.ProductSpeakerId
                    m_product.ProductLanguageId = m_product.BelongsTo.ProductLanguageId
                End If
            End If
            Return m_product
        End Get
    End Property
#End Region

#Region "Navigation"
    Private Sub Refresh()
        Response.Redirect(CSitemap.ProductEdit(Me.Product.ProductId))
    End Sub
    Private Sub ReturnToList()
        Response.Redirect(CSitemap.Products)
    End Sub
#End Region

#Region "Event Handlers - Page"
    Protected Overrides Sub PageInit()
        'Show Types
        With ddProductTypeId
            .DataTextField = "TypeName"
            .DataValueField = "TypeId"
            .DataSource = CProductType.Cache
            .DataBind()
        End With

        'Lock down type
        If TypeId > 0 AndAlso TypeId < 100 Then
            ddProductTypeId.Visible = False
            ddProductTypeId.Required = False
        Else
            ddProductTypeId.ValueInt = TypeId
        End If

        'Show parent set (if applicable)
        UnbindSideMenu()
        With Product
            If Not IsNothing(.ProductType) Then
                AddMenuSide("All " & Product.ProductType.Plural & "...", CSitemap.Products(Nothing, TypeId, Integer.MinValue, Integer.MinValue, Integer.MinValue, Integer.MinValue, Integer.MinValue, Nothing))
                If BelongsToId > 0 Then AddMenuSide("Back to Set...", CSitemap.ProductContents(BelongsToId))
                AddMenuSide("Create " & .TypeName)

                If .ShowDate Then txtCreated.ShowAmPm = True
                With .ProductType
                    Me.Title = "Create New " & .TypeName

                    ddProductBelongsToProductId.DataSource = CProduct.Cache.GetByTypeId(.TypeParentTypeId)
                    ddProductBelongsToProductId.DataBind()
                    If Not IsNothing(.ParentType) Then
                        ddProductBelongsToProductId.BlankItem("Select a " & .ParentType.TypeName)
                    Else
                        ddProductBelongsToProductId.BlankItem("Select a " & .TypeName)
                    End If
                    ddProductBelongsToProductId.ValueInt = BelongsToId
                    ddProductBelongsToProductId.Required = .TypeParentIsRequired
                    If IsNothing(.ParentType) Then ddProductBelongsToProductId.Visible = False
                End With
            Else
                AddMenuSide("All Products" & "...", CSitemap.Products(Nothing, TypeId, Integer.MinValue, Integer.MinValue, Integer.MinValue, Integer.MinValue, Integer.MinValue, Nothing))
                If BelongsToId > 0 Then AddMenuSide("Back to Set...", CSitemap.ProductContents(BelongsToId))
                AddMenuSide("Create Product")

                ddProductBelongsToProductId.Visible = False
                txtCreated.ShowAmPm = True
            End If
        End With


        'Load speaker & language (defaults are inherited from parent)
        ddSpeaker.DataSource = CSpeaker.Cache
        ddSpeaker.DataBind()

        ddLanguage.DataSource = CLanguage.Cache
        ddLanguage.DataBind()


        'Textbox logic
        Me.Form.DefaultFocus = txtProductName.Textbox.ClientID
        Me.Form.DefaultButton = btnSave.UniqueID
    End Sub
    Protected Overrides Sub PageLoad()
        If Page.IsPostBack Then Exit Sub

        LoadProduct()
    End Sub
#End Region

#Region "Event Handlers - Form"
    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        'If Not Me.IsValid() Then Exit Sub
        SaveProduct()
        Refresh()
    End Sub
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        ReturnToList()
    End Sub
#End Region

#Region "Private - Load/Save"
    Protected Sub LoadProduct()
        With Me.Product
            txtProductName.Text = .ProductName
            ddProductTypeId.ValueInt = .ProductTypeId
            ddProductBelongsToProductId.ValueInt = .ProductBelongsToProductId
            ddSpeaker.ValueInt = .ProductSpeakerId
            ddLanguage.ValueInt = .ProductLanguageId
            txtCreated.ValueDate = .ProductCreated
        End With
    End Sub
    Protected Sub SaveProduct()
        With Me.Product
            If ddProductTypeId.Visible Then .ProductTypeId = ddProductTypeId.ValueInt
            .ProductLanguageId = ddLanguage.ValueInt
            .ProductSpeakerId = ddSpeaker.ValueInt
            .ProductBelongsToProductId = ddProductBelongsToProductId.ValueInt
            .ProductName = txtProductName.Text
            .ProductCreated = txtCreated.ValueDate
            .Save()
        End With
    End Sub
#End Region


End Class
